package dao

import (
	"encoding/json"
	"time"
)

type l3net struct {
	L3_id int `json:"l3_id" gorm:"column:l3_id"`
	Uid int `json:"uid"`
	Uuid string `json:"uuid" gorm:"column:uuid"`
	Name string `json:"name" gorm:"column:name"`
	Description string `json:"description" gorm:"column:description"`
	Type string `json:"type" gorm:"column:type"`
	ZoneUuid string `json:"zone_uuid" gorm:"column:zoneUuid"`
	L2NetworkUuid string `json:"l2NetworkUuid" gorm:"column:l2NetworkUuid"`
	State string `json:"state" gorm:"column:state"`
	DnsDomain string `json:"dnsDomain" gorm:"column:dnsDomain"`
	Bc_l3netcol string `json:"bc_l3netcol" gorm:"column:bc_l3netcol"`
	System bool `json:"system" gorm:"column:system"`
	Category string `json:"category" gorm:"column:category"`
	IpVersion float64 `json:"ipVersion" gorm:"column:ipVersion"`
	Dns string `json:"dns" gorm:"column:dns"`
	IpRanges string `json:"ipRanges" gorm:"column:ipRanges"`
	NetworkServices string `json:"networkServices" gorm:"column:networkServices"`
	HostRoute string `json:"hostRoute" gorm:"column:hostRoute"`
	Ctime int64 `json:"c_time" gorm:"column:c_time"`
	Dtime int64 `json:"d_time" gorm:"column:d_time"`
}
func (t *l3net) Create(d map[string]interface{}) bool {

	var l3 l3net
	l3.Uuid = d["uuid"].(string)
	l3.Name = d["name"].(string)
	if v,ok := d["description"];ok {
		l3.Description = v.(string)
	}
	if v,ok := d["type"];ok {
		l3.Type = v.(string)
	}

	if v,ok := d["zoneUuid"];ok {
		l3.ZoneUuid = v.(string)
	}

	if v,ok := d["l2NetworkUuid"];ok {
		l3.L2NetworkUuid = v.(string)
	}

	if v,ok := d["state"];ok {
		l3.State = v.(string)
	}

	if d["dnsDomain"] != nil{
		l3.DnsDomain = d["dnsDomain"].(string)
	}

	//t.Bc_l3netcol = d["bc_l3netcol"].(string)
	l3.System = d["system"].(bool)
	l3.Category = d["category"].(string)
	l3.IpVersion = d["ipVersion"].(float64)
	if d["dns"] != nil{
		l3.Dns = d["dns"].(string)
	}
	if d["ipRanges"] != nil{
		ipRanges,err := json.Marshal(d["ipRanges"])
		if err != nil{
			panic(err)
		}
		l3.IpRanges = string(ipRanges)
	}
	if d["networkServices"] != nil{
		networkServices ,err := json.Marshal(d["networkServices"])
		if err != nil{
			panic(err)
		}
		l3.NetworkServices = string(networkServices)
	}
	if d["hostRoute"] != nil{
		hostRoute , err := json.Marshal(d["hostRoute"])
		if err != nil{
			panic(err)
		}
		l3.HostRoute = string(hostRoute)
	}
	l3.Uid = d["uid"].(int)
	l3.Ctime = time.Now().Unix()

	err := db.Create(&l3).Error
	b := true
	if err != nil{
		b = false
	}
	return b

}
func (t *l3net)Delete(w map[string]interface{}) bool {

	err := db.Where(w).Delete(&t).Error
	b := true
	if err != nil{
		b = false
	}
	return b

}
var L3Net = &l3net{}
